package com.ruoyi.yz.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.yz.util.MemberStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.yz.mapper.YzMemberMapper;
import com.ruoyi.yz.domain.YzMember;
import com.ruoyi.yz.service.IYzMemberService;
import com.ruoyi.common.core.text.Convert;

/**
 * 参会人员Service业务层处理
 *
 * @author yz
 * @date 2024-06-14
 */
@Service
public class YzMemberServiceImpl implements IYzMemberService
{
    @Autowired
    private YzMemberMapper yzMemberMapper;

    /**
     * 查询参会人员
     *
     * @param id 参会人员主键
     * @return 参会人员
     */
    @Override
    public YzMember selectYzMemberById(Long id)
    {
        return yzMemberMapper.selectYzMemberById(id);
    }

    /**
     * 查询参会人员列表
     *
     * @param yzMember 参会人员
     * @return 参会人员
     */
    @Override
    public List<YzMember> selectYzMemberList(YzMember yzMember)
    {
        return yzMemberMapper.selectYzMemberList(yzMember);
    }

    /**
     * 新增参会人员
     *
     * @param yzMember 参会人员
     * @return 结果
     */
    @Override
    public int insertYzMember(YzMember yzMember)
    {
        if (!checkUnique(yzMember)) {
            throw new ServiceException("人员院校信息已经存在，请勿重复提交！");
        }
        yzMember.setCreateTime(DateUtils.getNowDate());
        return yzMemberMapper.insertYzMember(yzMember);
    }

    /**
     * 修改参会人员
     *
     * @param yzMember 参会人员
     * @return 结果
     */
    @Override
    public int updateYzMember(YzMember yzMember)
    {
        YzMember query = yzMemberMapper.selectYzMemberById(yzMember.getId());
        if (MemberStatus.SIGNED.getCode().equals(yzMember.getStatus()) && Objects.isNull(query.getSigninTime())) {
            yzMember.setSigninTime(DateUtils.getNowDate());
        }
        yzMember.setUpdateTime(DateUtils.getNowDate());
        return yzMemberMapper.updateYzMember(yzMember);
    }

    /**
     * 批量删除参会人员
     *
     * @param ids 需要删除的参会人员主键
     * @return 结果
     */
    @Override
    public int deleteYzMemberByIds(String ids)
    {
        return yzMemberMapper.deleteYzMemberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除参会人员信息
     *
     * @param id 参会人员主键
     * @return 结果
     */
    @Override
    public int deleteYzMemberById(Long id)
    {
        return yzMemberMapper.deleteYzMemberById(id);
    }

    @Override
    public boolean checkUnique(YzMember yzMember) {
        YzMember query = new YzMember();
        query.setConferenceId(yzMember.getConferenceId());
        query.setName(yzMember.getName());
        query.setSchoolName(yzMember.getSchoolName());

        return yzMemberMapper.selectYzMemberList(query).size() == 0;
    }

    @Override
    public int signIn(Long id) {
        return yzMemberMapper.signIn(id);
    }
}
